﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.ComponentModel;

namespace AtomITBackupUtility
{
    public static class Files
    {
        
        /// <summary>
        /// Copy a complete directory
        /// </summary>
        /// <param name="url_source">source directory</param>
        /// <param name="url_destine">destination directory</param>
        /// <param name="overwrite">whether to overwrite</param>
        public static void CopyDirectory(string url_source, string url_destine, bool overwrite, Form formToupdate, BackgroundWorker worker)
        {
  
            if ((overwrite) && (Directory.Exists(url_destine)))
                Directory.Delete(url_destine, true);

            if (!Directory.Exists(url_destine))
                Directory.CreateDirectory(url_destine);

            string path_file_destine = string.Empty;

            int noOfFiles = 0;
            int complete = 0;

            // calculate no of files

            string[] files = Directory.GetFiles(url_source, "*", SearchOption.AllDirectories);

            noOfFiles = files.Length;

            // use files to calculate %

            double percent = 0;

            foreach (string file in Directory.GetFiles(url_source))
            {
                string fileName = file.Replace(url_source, "");

                path_file_destine = url_destine + @"\" + Path.GetFileName(file);

                File.Copy(file, path_file_destine, true);

                complete++;

                percent = ((double)complete / (double)noOfFiles) * 100;

                worker.ReportProgress((int)percent, url_destine);

            }

            foreach (string sub_dir in Directory.GetDirectories(url_source))
                CopyDirectory(sub_dir + "/",url_destine + Path.GetFileName(sub_dir) + "/", overwrite, formToupdate, worker);

        }


    }
}
